#pragma once
#include <iostream>
#include <pthread.h>
#include <functional>
#include <string>
#include <sys/syscall.h>
#include <unistd.h>

#define get_lwp_id() syscall(SYS_gettid)

using func_t = std::function<void()>;
const static std::string defaultname = "None-name";

class Thread
{
public:
    Thread(func_t func, const std::string &name = defaultname)
        : _name(name), _func(func), _isrunning(false)
    {
        std::cout << "线程初始化成功" << std::endl;
    }
    static void *routine(void *args)
    {
        Thread *th = static_cast<Thread *>(args);
        th->_func();
        th->_lwp = get_lwp_id();
        th->_isrunning = true;
        pthread_exit((void *)0);
    }
    void Start()
    {
        pthread_create(&_tid, nullptr, routine, this);
    }
    void Join()
    {
        pthread_join(_tid, nullptr);
    }
    ~Thread() {}

private:
    pthread_t _tid;
    std::string _name;
    func_t _func;
    pid_t _lwp;
    bool _isrunning;
};